﻿// /* **********************************************************************************
//  *
//  * Copyright (c) Sky Sanders. All rights reserved.
//  * 
//  * This source code is subject to terms and conditions of the Microsoft Public
//  * License (Ms-PL). A copy of the license can be found in the license.htm file
//  * included in this distribution.
//  *
//  * You must not remove this notice, or any other, from this software.
//  *
//  * **********************************************************************************/
using System;
using System.Threading;

namespace Salient.Spikes
{
    class Program
    {
        /// <summary>
        /// Exercises the ControllableProcess class
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            
            var attach = new Action<ControllableProcess>((c) =>
            {
                c.HasErrorChar += ((s, e) =>
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write(e.Char);
                    Console.ResetColor();
                });
                c.HasOutputChar += ((s, e) =>
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write(e.Char);
                    if (e.Char == '\n')
                    {

                    }
                    Console.ResetColor();
                });
            });

            Console.WriteLine("Valid command should output StandardOut (green)\r\n");
            ControllableProcess cp = new ControllableProcess("net", "share", Environment.CurrentDirectory);

            attach(cp);
            cp.Start();
            Thread.Sleep(1000);
            int exitCode = cp.Stop();
            Console.WriteLine("ExitCode={0}\r\n\r\n", exitCode);


            Console.WriteLine("Invalid command should output StandardErr (red)\r\n");
            cp = new ControllableProcess("net", "shares", Environment.CurrentDirectory);
            attach(cp);
            cp.Start();
            Thread.Sleep(1000);
            cp.Stop();
            Console.WriteLine("ExitCode={0}\r\n\r\n", exitCode);


            Console.WriteLine("Interact with FTP server\r\n");
            cp = new ControllableProcess("DemoServer.exe", null, Environment.CurrentDirectory);
            attach(cp);
            cp.Start();

            Thread.Sleep(1000);
            cp.SetInput("open" + Environment.NewLine);
            Thread.Sleep(1000);

            cp.SetInput("192.168.1.102" + Environment.NewLine);
            Thread.Sleep(1000);

            cp.SetInput("anonymous" + Environment.NewLine);
            Thread.Sleep(1000);


            // got password prompt and sent password
            cp.SetInput("me@my.com" + Environment.NewLine);
            Thread.Sleep(5000);

            // not sure why not getting a listing, but that is not the point.
            cp.SetInput("ls" + Environment.NewLine);
            Thread.Sleep(1000);

            cp.SetInput("bye" + Environment.NewLine);
            Thread.Sleep(1000);

            cp.Stop();
            Console.WriteLine("ExitCode={0}\r\n\r\n", exitCode);

            // not sure why I am not getting a listing from the ftp
            // server but the ability to interact with the process is
            // shown. that is the point really. I guess I should write a 
            // dummy prog to test fully.

            Console.WriteLine("press enter to exit");
            Console.ReadLine();
        }

    }
}
